<script setup>
import { ref } from 'vue'
import { initializeFiledData } from '@/views/readExcelHeader/globalConfigData/allFiledConfig'
import { getSheetHeadersWithFormulas } from '@/utils/excelSheet'
import ImportExcelHeader from './components/ImportExcelHeader/index.vue'
import previewTheRecognitionHeader from './components/previewTheRecognitionHeader/index.vue'
const importExcelVisible = ref(false)
const previewHeaderVisible = ref(false)
const ruleConfiguration = ref([])

const fileInfo = ref(null)
const getExcelHeader = async info => {
  if (!info) {
    importExcelVisible.value = false
    return
  }
  fileInfo.value = info
  await mergeSystemHeaders({ fileInfo: info, flag: false })
  importExcelVisible.value = false
  previewHeaderVisible.value = true
}

const getRuleConfiguration = () => {
  return ruleConfiguration.value
}

//匹配表头
const matchTheHeaders = info => {
  const { itemGroupName, insertSelectedRows } = info
  console.log('info', info)
  //将已经选择的表头导入到当前组
  const index = ruleConfiguration.value.findIndex(item => item?.itemGroupName === itemGroupName)

  console.log('insertSelectedRows 选中的表头', insertSelectedRows)

  if (index !== -1) {
    ruleConfiguration.value[index].salaryItems = ruleConfiguration.value[index].salaryItems.concat(insertSelectedRows)
  }

  previewHeaderVisible.value = false
}

//将表格识别的表头与系统表头合并
const identifiedHeaderInformation = ref([])

const mergeSystemHeaders = async ({ fileInfo, flag }) => {
  const { filedDataFlapMap } = await initializeFiledData()
  //已经配置表头
  const configurationMap = new Map(
    getRuleConfiguration()
      .map(item => item.salaryItems)
      .flat()
      .map(salaryItem => [salaryItem.itemName, salaryItem])
  )

  const headers = await getSheetHeadersWithFormulas({
    file: fileInfo.file,
    sheetNo: fileInfo.sheetNo,
    flag
  })

  identifiedHeaderInformation.value = headers.map(item => {
    const res = filedDataFlapMap[item.header]
    return {
      itemName: item.header,
      disabled: configurationMap.has(item.header),
      // * @filedType: string 字段类型 system: 系统项  import: 导入项   calculate 计算项
      filedType: (() => {
        if (item.formulaHtml) {
          return 'calculate'
        } else if (res && !['system'].includes(res.filedType)) {
          return res.filedType || 'import'
        } else {
          return 'import'
        }
      })(),
      valueType: (() => {
        if (res) {
          return res.valueType
        } else {
          return 'int'
        }
      })(),
      value: item.header,
      dataMessage: item.formula || null,
      formulaHtml: item.formulaHtml || null,
      //系统信息表头信息
      systemInfo: res
    }
  })
}

const mergeHeaders = async flag => {
  await mergeSystemHeaders({ fileInfo: fileInfo.value, flag })
}
</script>

<template>
  <div>
    <a-button @click="importExcelVisible = true">读取表头和公式</a-button>
  </div>

  <!--导入表头-->
  <ImportExcelHeader v-model:visible="importExcelVisible" @closeModal="getExcelHeader" />

  <!--表头预览-->
  <previewTheRecognitionHeader
    :ruleConfiguration="ruleConfiguration"
    @handOk="matchTheHeaders"
    :headers="identifiedHeaderInformation"
    v-model:visible="previewHeaderVisible"
    @mergeHeaders="mergeHeaders"
  />
</template>

<style scoped></style>
